/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Customer.java
 *
 * Created on Oct 27, 2010, 4:59:24 PM
 */

package lmclogistics;

import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sonia Tan
 */
public class EditCustomer extends javax.swing.JFrame {

    /** Creates new form Customer */
    private Image bg;
    private int id;

    Text cd = new Text();
    TextArea name = new TextArea();
        TextArea address = new TextArea();
        TextArea vat = new TextArea();
        TextArea telNo = new TextArea();
        TextArea begBal = new TextArea();
        TextArea tinNo = new TextArea();

        ButtonGroup active = new ButtonGroup();
        final JRadioButton activeY = new JRadioButton();
        final JRadioButton activeN = new JRadioButton();

    public EditCustomer(int sID) {
        id = sID;

        setLayout(null);
        setTitle("Edit Customer");
        setAlwaysOnTop(true);
        setSize(512, 488);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        centerScreen();
        setVisible(true);
        
        setBg();
        okCancelButtons();
        initText();
        initFlds();

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public final void setBg(){
        bg = new Image("panel.png",509,460);

        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
        NButton cancel = new NButton("Cancel",410,420);
        NButton ok = new NButton("Ok",320 ,420);
        add(ok,0);
        add(cancel,0);
        cancel.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    }
                });
        ok.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    acceptEdit();
                    dispose();
                    //FormWarning warn = new FormWarning("incorrect");
                    }
                });
    }

    public void initText(){
        Text code = new Text();        
        Text name = new Text();
        Text address = new Text();
        Text vat = new Text();
        Text telNo = new Text();
        Text begBal = new Text();
        Text active = new Text();
        Text tinNo = new Text();

        code.inText("Code:", 15, 15);
        //cd.inText("ABCD",100,15);
        name.inText("Name:", 15, 40);
        address.inText("Address:", 15, 65);
        vat.inText("VAT:", 320, 15);
        telNo.inText("Tel. No.:", 15, 120);
        begBal.inText("BEG. BAL", 15, 175);
        active.inText("Active:", 15, 200);
        tinNo.inText("TIN. No.:", 15, 225);

        add(code,0);
        add(name,0);
        add(address,0);
        add(vat,0);
        add(telNo,0);
        add(begBal,0);
        add(active,0);
        add(tinNo,0);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void initFlds() {
        String query = "SELECT * FROM customer WHERE custCode = "+id;
        String qCode;
        String qName;
        String qAddr;
        String qVat;
        String qTelNo;
        String qBegBal;
        String qTinNo;
        String qActive;

        name.initText(60, 25, 100, 40);
        address.initText(60, 25, 100, 65);
        vat.initText(60, 25, 355, 15);
        telNo.initText(60, 25, 100, 120);
        begBal.initText(60, 25, 100, 175);
        tinNo.initText(60, 25, 100, 225);

        activeY.setText("Yes");
        activeN.setText("No");
        active.add(activeY);
        active.add(activeN);
        activeY.setLocation(100,200);
        activeN.setLocation(150,200);
        activeY.setSize(50,25);
        activeN.setSize(50,25);

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);

            if(rs.next())
            {
                qCode = rs.getString("custCode");
                qName = rs.getString("custName");
                qBegBal = rs.getString("custBegBal");
                qAddr = rs.getString("custAddr");
                qTelNo = rs.getString("custTelNo");
                qVat = rs.getString("custVat");
                qTinNo = rs.getString("custTinNo");
                qActive = rs.getString("custActive");

                cd.inText(qCode, 100, 15);
                name.setText(qName);
                begBal.setText(qBegBal);
                address.setText(qAddr);
                telNo.setText(qTelNo);
                vat.setText(qVat);
                tinNo.setText(qTinNo);
                if(qActive.equals("1"))
                    activeY.setSelected(true);
                else
                    activeN.setSelected(true);
            }

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EditChassis.class.getName()).log(Level.SEVERE, null, ex);
        }

        add(name,0);
        add(activeY,0);
        add(activeN,0);
        add(address,0);
        add(vat,0);
        add(telNo,0);
        add(begBal,0);
        add(tinNo,0);
        add(cd,0);
    }


    private void acceptEdit()
    {
        String cActive = "1";

        if(activeY.isSelected()==true)
            cActive = "1";
        if(activeN.isSelected()==true)
            cActive = "0";

        String query = "UPDATE customer SET custName='"+name.getText()+"', custAddr='"+address.getText()+
                "', custVat='"+vat.getText()+"', custTelNo='"+telNo.getText()+"', custBegBal='"+begBal.getText()+
                "', custActive='"+cActive+"', custTinNo='"+tinNo.getText()+"' WHERE custCode="+id;

        try{
            Connection connection = Sql.getSqlConnection();
            PreparedStatement pstmt = connection.prepareStatement(query);
            pstmt.executeUpdate();

            Sql.closeSqlConnection(connection);
        } catch(Exception ex)
        {
            Logger.getLogger(EditCustomer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
